using System;
using System.Data;
using System.Data.SqlClient;
using CoreLibrary.Sql;

namespace CoreLibrary.Messaging.Sql {

    public class GetMessageStatus : ISqlScalar<MessageStatus?> {

        private readonly string _tableName;
        private readonly string _messageId;

        public GetMessageStatus(string tableName, string messageId) {
            _tableName = tableName;
            _messageId = messageId;
        }

        public string GetSQL() {
            return string.Format(@"
                select status
                from {0}
                where id = @id
            ", _tableName);
        }

        public void SetParameters(SqlCommand command) {
            command.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = new Guid(_messageId);
        }

        public MessageStatus? GetResult(SqlConnection conn, object result) {
            return result == null || result == DBNull.Value ? (MessageStatus?)null : (MessageStatus)result;
        }

    }
}